﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.EntityModel;
using YasharEl.Infrastructure.Core.Prototype;
using YasharEl.Infrastructure.Core.EntityModel.Prototype;
using YasharEl.Infrastructure.Core.Commenting;
using YasharEl.Infrastructure.TaskEngine.EntityModel.Prototype;
using YasharEl.Infrastructure.TaskEngine.Assignments;
using System.ComponentModel.DataAnnotations;
using YasharEl.Infrastructure.TaskEngine.EntityModel.Metadata;

namespace YasharEl.Infrastructure.TaskEngine.EntityModel
{
    [MetadataType(typeof(TaskEntityMetadata))]
    public class TaskEntity : AbstractPrototypeEntity, ITask
    {
        #region Fields

        private string _initiatorCorrelationId;

        #endregion

        #region Entity Overrides

        public override long Id
        {
            get
            {
                return TaskId;
            }
            protected set
            {
                TaskId = value;
            }
        }

        #endregion

        #region ITask Members

        public virtual long TaskId
        {
            get;
            set;
        }

        public virtual ITaskDefinition TaskDefinition
        {
            get;
            private set;
        }

        public virtual string TaskName { get; set; }
        public virtual string TaskDescription { get; set; }
        public virtual Guid? InitiatorCorrelationId
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_initiatorCorrelationId))
                    return null;
                return new Guid(_initiatorCorrelationId);
            }
            set
            {
                if (value.HasValue)
                {
                    _initiatorCorrelationId = value.Value.ToString();
                }
                else
                {
                    _initiatorCorrelationId = null;
                }
            }
        }

        public virtual TaskPriority TaskPriority { get; set; }
        public virtual TaskStatus TaskStatus { get; set; }
        public virtual TaskStatusReason TaskStatusReason { get; set; }
        public virtual DateTime CreatedDate { get; set; }
        public virtual ITaskParticipant CreatedBy { get; set; }
        public virtual DateTime? ModifiedDate { get; set; }
        public virtual ITaskParticipant ModifiedBy { get; set; }

        public virtual ParticipantType ParticipantType { get; set; }
        public virtual bool IsDataModified { get; set; }
        public virtual ITaskParticipant ConsultWith { get; set; }
        public virtual DateTime? ReminderDate { get; set; }

        public virtual IList<ITaskAssignment> CurrentAssignments
        {
            get;
            set;
        }

        public virtual IList<ITaskAction> TaskActions { get; private set; }

        #endregion

        #region AbstractPrototypeEntity Overrides

        protected override IHasPropertyDefinitions MappingsContainer
        {
            get
            {
                return TaskDefinition;
            }
        }

        protected override AbstractPropertyInstanceEntity CreatePropertyInstanceEntity()
        {
            return new TaskPropertyInstanceEntity();
        }

        #endregion

        #region ICommentable Members

        public IComment AddComment(string comment)
        {
            throw new NotImplementedException();
        }

        public virtual IList<IComment> Comments
        {
            get;
            set;
        }

        #endregion
    }
}
